<template>
  <q-input
    v-model="formatTime"
    filled
    mask="datetime"
    :label="field.label"
    lazy-rules
    :rules="rules"
    :readonly="field.readonly"
    bottom-slots
  >
    <template v-slot:prepend v-if="!field.readonly">
      <q-icon
        name="event"
        class="cursor-pointer"
      >
        <q-popup-proxy
          ref="qDateProxy"
          transition-show="scale"
          transition-hide="scale"
        >
          <q-date
            mask="YYYY/MM/DD HH:mm"
            v-model="formatTime"
          />
        </q-popup-proxy>
      </q-icon>
    </template>
    <template v-slot:append v-if="!field.readonly">
      <q-icon name="access_time" class="cursor-pointer">
        <q-popup-proxy
          ref="qTimeProxy"
          transition-show="scale"
          transition-hide="scale">
          <q-time
            mask="YYYY/MM/DD HH:mm"
            v-model="formatTime"
            format24h
          />
        </q-popup-proxy>
      </q-icon>
    </template>

    <template v-slot:hint v-if="field.hint || field.compare">
      <div class="text-bold text-white bg-yellow-10" style="padding: 1px;" v-if="canShowCompare">
        {{field.compare.label ? field.compare.label + '：' : ''}}{{field.compare.map ? field.compare.map.value : field.compare.value}}
      </div>
      <div v-if="field.hint">{{field.hint}}</div>
    </template>
  </q-input>
</template>
<script>
import { defineComponent } from 'vue';
import { date } from 'quasar';

export default defineComponent({
  name: 'dateInput',
  props: {
    field: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    const self = this;
    return {

    }
  },
  methods: {
  },
  computed: {
    formatTime: {
      get () {
        return date.formatDate(this.field.value * 1000, 'YYYY/MM/DD HH:mm');
      },
      set (val) {
        this.field.value = (new Date(val)).valueOf() / 1000;
        this.$refs.qDateProxy.hide();
        this.$refs.qTimeProxy.hide();
      }
    },
    rules () {
      return this.field.rules ? [...this.field.rules] : ['datetime'];
    },
    canShowCompare () {
      /*
        compare的值
        compare: {
          label, // 标题
          value, // 对比的值
          always // true-有数据就显示 false-值不同，再显示
        } */
      return this.field.compare && this.field.compare.value !== null && (this.field.compare.always || this.field.compare.value != this.field.value);
    }
  }
});
</script>
